﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Ports;



namespace enhanced_command_interface
{
    class MovementController
    {
        private static class DirectionCommands
        {
            public static string northWest = "wa" +'\n';
            public static string north = "ww" + '\n';
            public static string northEast = "wd" + '\n';

            public static string west = "aa" + '\n';
            public static string east = "dd" + '\n';

            public static string southWest = "sa" + '\n';
            public static string south = "ss" + '\n';
            public static string southEast = "sd" + '\n';

            public static string stop = "ff" + '\n';
        }

        private static class Constants
        {
            public const int NUM_OF_DIRECTIONS = 9;
        }

        public enum Directions { North, NorthEast, NorthWest, East, West, South, SouthEast, SouthWest, Stop };

        private SerialPort serialPort = new SerialPort();
        private Directions currentMovingDirection = Directions.Stop;


        //public void setSerial(string portName, int baudRate)
        //{
        //    try
        //    {
        //        serialPort.PortName = portName;
        //        serialPort.BaudRate = baudRate;
        //        serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
        //        serialPort.Open();
        //    }

        //    catch (Exception ex)
        //    {
        //        //  MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //    }

        //}

        public SerialPort getSerial() //Up to user to set the port and open it, change later?
        {
            return serialPort;
        }

        public Directions getMovementDirection()
        {
            return this.currentMovingDirection;
        }

        public bool MoveNorthWest()
        {
            if (serialPort.IsOpen == false)
                return false;

            this.currentMovingDirection = Directions.NorthWest;
            serialPort.Write(DirectionCommands.northWest);
            return true;
        }

        public bool MoveNorth()
        {
            if (serialPort.IsOpen == false)
                return false;

            this.currentMovingDirection = Directions.North;
            serialPort.Write(DirectionCommands.north);
            return true;
        }

        public bool MoveNorthEast()
        {
            if (serialPort.IsOpen == false)
                return false;

            this.currentMovingDirection = Directions.NorthEast;
            serialPort.Write(DirectionCommands.northEast);
            return true;
        }

        public bool MoveWest()
        {
            if (serialPort.IsOpen == false)
                return false;

            this.currentMovingDirection = Directions.West;
            serialPort.Write(DirectionCommands.west);
            return true;
        }

        public bool MoveEast()
        {
            if (serialPort.IsOpen == false)
                return false;

            this.currentMovingDirection = Directions.East;
            serialPort.Write(DirectionCommands.east); 
            return true;
        }


        public bool MoveSouthWest()
        {
            if (serialPort.IsOpen == false)
                return false;

            this.currentMovingDirection = Directions.SouthWest;
            serialPort.Write(DirectionCommands.southWest);
            return true;
        }

        public bool MoveSouth()
        {
            if (serialPort.IsOpen == false)
                return false;

            this.currentMovingDirection = Directions.South;
            serialPort.Write(DirectionCommands.south); 
            return true;
        }

        public bool MoveSouthEast()
        {
            if (serialPort.IsOpen == false)
                return false;

            this.currentMovingDirection = Directions.SouthEast;
            serialPort.Write(DirectionCommands.southEast);
            return true;
        }

        public bool StopMovement()
        {
            if (serialPort.IsOpen == false)
                return false;

            this.currentMovingDirection = Directions.Stop;
            serialPort.Write(DirectionCommands.stop);
            return true;
        }

    }
}
